package com.example.swd1.gudong.main.sportrang;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class SquareListViewAdapter extends BaseAdapter {
    private Context context;
    private ViewPager vp_square_head;
    private ListView lv_square_content;
    private SquareViewPagerAdapter squareAdapter;
    private SquareContentAdapter contentAdapter;
    private int[] imageResources = new int[]{R.drawable.square_head_1, R.drawable.square_head_2, R.drawable.square_head_3, R.drawable.square_head_4, R.drawable.square_head_5};
    private String[] titles = new String[]{Constant.SQUARE_TITLE_BROAD_CASE, Constant.SQUARE_TITLE_HOT, Constant.SQUARE_TITLE_NEAR, Constant.SQUARE_TITLE_WAKEUP, Constant.SQUARE_TITLE_HEALTH};
    private int[] broadCase = new int[]{R.drawable.square_1, R.drawable.square_2, R.drawable.square_3};
    private int[] hot = new int[]{R.drawable.square_4, R.drawable.square_5, R.drawable.square_6};
    private int[] near = new int[]{R.drawable.square_7, R.drawable.square_8, R.drawable.square_9};
    private int[] wakeUp = new int[]{R.drawable.square_10, R.drawable.square_11, R.drawable.square_12};
    private int[] health = new int[]{R.drawable.square_13, R.drawable.square_14, R.drawable.square_15};

    public SquareListViewAdapter(Context context) {
        this.context = context;
    }

    private ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.item_fragment_sport_rang_square, null);
        vp_square_head = (ViewPager) convertView.findViewById(R.id.vp_square_head);
        lv_square_content = (ListView) convertView.findViewById(R.id.lv_square_content);
        lv_square_content.setFooterDividersEnabled(false);
        squareAdapter = new SquareViewPagerAdapter(loadImageResource());
        vp_square_head.setAdapter(squareAdapter);
        vp_square_head.setOnPageChangeListener(listener);
        contentAdapter = new SquareContentAdapter(context, loadContentResource());
        lv_square_content.setAdapter(contentAdapter);
        return convertView;
    }

    private List<ImageView> loadImageResource() {
        List<ImageView> images = new ArrayList<>();
        for (int i = 0; i < imageResources.length; i++) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imageResources[i]);
            images.add(imageView);
        }
        return images;
    }

    private List<Map<String, Object>> loadContentResource() {
        List<Map<String, Object>> contents = new ArrayList<>();
        int[][] iamges = new int[][]{broadCase, hot, near, wakeUp, health};
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            Map<String, Object> content = new HashMap<>();
            content.put(Constant.SQUARE_KEY_TITLE, titles[i]);
            content.put(Constant.SQUARE_KEY_IMAGE, iamges[i]);
            contents.add(content);

        }
        return contents;

    }

}
